package com.qfedu.mtl.controller;

import com.alibaba.fastjson.JSON;
import com.qfedu.mtl.entity.Good;
import com.qfedu.mtl.query.GoodQuery;
import com.qfedu.mtl.service.GoodService;
import com.qfedu.mtl.service.impl.GoodServiceImpl;
import com.qfedu.mtl.util.CharacterUtil;
import com.qfedu.mtl.util.HttpUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.page.Pager;
import com.qfedu.mtl.util.web.annotation.Controller;
import com.qfedu.mtl.util.web.annotation.RequestMapping;
import com.qfedu.mtl.vo.TableVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Date 2023/9/26 17:42
 */
@Controller
@RequestMapping("/good")
public class GoodController {
    private GoodService goodsService=new GoodServiceImpl();

    //分页查询
    @RequestMapping("/list.do")
    public void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        String page = StrUtil.ifNull(req.getParameter("page" ), "1");
        String size = StrUtil.ifNull(req.getParameter("size"), "10");
        GoodQuery query = HttpUtil.request2Bean(req, GoodQuery.class);
        Pager<Good> pager = goodsService.findPage(Integer.valueOf(page), Integer.valueOf(size), query);

        TableVO vo = TableVO.me().setData(pager.getRows()).setCount(pager.getTotal());
        String json = JSON.toJSONString(vo);
        resp.getWriter().write(json);
    }
    //查询单条记录
    @RequestMapping("/find.do")
    public void find(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        String id = req.getParameter("id");
        Good good = goodsService.findById(Integer.valueOf(id));
        HttpUtil.responseSuccess(good,resp);

    }
    //增加记录
    @RequestMapping("/add.do")
    public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        CharacterUtil.setContentWithJson(req, resp);
        Good good = HttpUtil.request2Bean(req, Good.class);
        try {
            goodsService.save(good);
            HttpUtil.responseSuccess(null,resp);
        }catch (Exception e){
            HttpUtil.response(null,-1,e.getMessage(),resp);
        }
    }
    //删除单条记录
    @RequestMapping("/delete.do")
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        String id = req.getParameter("id");
        if (StrUtil.isNullOrEmpty(id)){
            HttpUtil.response(null,-1,"请指定删除的ID",resp);
            return;
        }
        try {
            goodsService.delete(Integer.valueOf(id));
            HttpUtil.responseSuccess(null,resp);
        }catch (Exception e){
            HttpUtil.response(null,-1,e.getMessage(),resp);
        }
    }
    //批量删除
    @RequestMapping("/deleteBatch.do")
    public void deleteBatch(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        String ids = req.getParameter("ids");
        if (StrUtil.isNullOrEmpty(ids)){
            HttpUtil.response(null,-1,"请输入要删除的ID集合",resp);
            return;
        }
        String[] idss = ids.split(",");
        List<Integer> idsss = Stream.of(idss).map(e -> Integer.valueOf(e)).collect(Collectors.toList());
        try {
            goodsService.deleteBatch(idsss);
            HttpUtil.responseSuccess(null,resp);
        }catch (Exception e){
            HttpUtil.response(null,-1,e.getMessage(),resp);
        }
    }
    //更新
    @RequestMapping("/update.do")
    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        Good good = HttpUtil.request2Bean(req, Good.class);
        try {
            goodsService.update(good);
            HttpUtil.responseSuccess(null,resp);
        }catch (Exception e){
            HttpUtil.response(null,-1,e.getMessage(),resp);
        }
    }

}
